package com.yalantis.ucrop.view;


import com.yalantis.ucrop.util.LogUtils;
import com.yalantis.ucrop.util.NumCalcUtil;
import com.yalantis.ucrop.util.RotationGestureDetector;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ScaleInfo;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

/**
 * Created by Oleksii Shliama (https://github.com/shliama).
 */
public class GestureCropImageView extends CropImageView implements Component.TouchEventListener {
    //暂不提供手势
    private RotationGestureDetector mRotateDetector;
    ScaleListener mScaledListener;
    private float before_X = -1;
    private float before_Y = -1;

    private float mMidPntX, mMidPntY;
    public GestureCropImageView(Context context, AttrSet attrs) {
        super(context, attrs, "0");
    }

    @Override
    protected void init() {
        super.init();
        setupGestureListeners();
    }

    private void setupGestureListeners() {
        //旋转监听
        mRotateDetector = new RotationGestureDetector(new RotateListener());

        //触摸监听
        setTouchEventListener(this);
        //缩放监听
        mScaledListener = new ScaleListener();
        setScaledListener(mScaledListener);
    }
    //由于滑动监听缺陷，所以滑动操作整合在触摸监听里
    @Override
    public boolean onTouchEvent(Component component, TouchEvent event) {
        if ((event.getAction() ) == TouchEvent.PRIMARY_POINT_DOWN) {
            cancelAllAnimations();
        }

        LogUtils.LogInfo("qqqqqqq:","Pointer Counter:"+event.getPointerCount());
        if (event.getPointerCount() > 1) {
            //获取缩放中点
            mMidPntX = (NumCalcUtil.add(event.getPointerPosition(0).getX() , event.getPointerPosition(1).getX()) / 2);
            mMidPntY = (NumCalcUtil.add(event.getPointerPosition(0).getY() , event.getPointerPosition(1).getY()) / 2);
        }
        if(event.getPointerCount() == 1){
            //滑动
            float now_X = event.getPointerPosition(0).getX();
            float now_Y = event.getPointerPosition(0).getY();
            if(before_X > 0 && before_Y >0){
                float scroll_X = NumCalcUtil.subtract(now_X , before_X);
                float scroll_Y = NumCalcUtil.subtract(now_Y , before_Y);
                postTranslate(scroll_X,scroll_Y);
            }
            before_X = now_X;
            before_Y = now_Y;
            if(event.getAction() ==TouchEvent.PRIMARY_POINT_UP){
                before_X = -1;
                before_Y = -1;
            }
        }

//        mGestureDetector.onTouchEvent(event);

        System.out.println();
        //暂不提供手势
        mRotateDetector.onTouchEvent(event);

        if ((event.getAction() ) == TouchEvent.PRIMARY_POINT_UP) {
            setImageToWrapCropBounds();
        }
        return true;
    }

    private class ScaleListener implements ScaledListener {


        @Override
        public void onScaleStart(Component component, ScaleInfo scaleInfo) {

        }

        @Override
        public void onScaleUpdate(Component component, ScaleInfo scaleInfo) {
            mMidPntX = scaleInfo.updatePoint.position[0];
            mMidPntY = scaleInfo.updatePoint.position[1];
            postScale((float) (0.1f *scaleInfo.scale), mMidPntX, mMidPntY);
//            component.setScale((float) scaleInfo.scale,(float) scaleInfo.scale);
        }

        @Override
        public void onScaleEnd(Component component, ScaleInfo scaleInfo) {

        }
    }



    private class RotateListener extends RotationGestureDetector.SimpleOnRotationGestureListener {

        @Override
        public void onRotation(RotationGestureDetector rotationDetector) {
            postRotate(rotationDetector.getAngle(), mMidPntX, mMidPntY);
        }

    }

}
